package sfsoftware.game.actor;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import sfsoftware.game.TrevorLegacy;
import sfsoftware.game.actor.attributes.Collidable;
import sfsoftware.game.actor.item.Item;
import sfsoftware.game.trevor.Player;
import sfsoftware.game.trevor.ui.Dialog;
import sfsoftware.util.Actor;
import sfsoftware.util.Clock;
import sfsoftware.util.Updatable;

public class PushableObject extends Actor implements Updatable
{

	public static Properties properties = new Properties();	
	public static final String FAILED_PUSH_MESSAGE;
	
	private Clock pushClock = new Clock(20);
	
	static 
	{		
		try 
		{
            //load a properties file
			properties.load(new FileInputStream("res/game.properties"));			
		} 
		catch (IOException ex) 
		{			
			ex.printStackTrace();
		}	
		// Property containing the text we will display if the user is unable to push
		FAILED_PUSH_MESSAGE = properties.getProperty("message.tooheavy");
	}
	
	public PushableObject(int destX, int destY, String texture, int destWidth, int destHeight) 
	{
		super(destX, destY, texture, destWidth, destHeight);
		setSpeed(1);
		setWalkable(false);
	}

	public PushableObject(int destX, int destY, String texture, int srcX, int srcY, int destWidth, int destHeight) 
	{
		super(destX, destY, texture, srcX, srcY, destWidth, destHeight);
		setSpeed(1);
		setWalkable(false);
	}
	
	@Override
	public void collisionResult(Collidable object) 
	{				
		// Only the player can move pushable objects.
		if(object.equals(TrevorLegacy.player))
		{								
			if(!(Player.getInventory(Item.PASSIVE).contains("Strength Ring")))
			{
				// It's too heavy to move!
				Dialog.raiseDialog(FAILED_PUSH_MESSAGE);			
			}
			// If they have the strength ring, move the rock every 20ms... we want it to feel heavy ;)
			else if(pushClock.updateClock())
			{
				if(this.move(TrevorLegacy.player.getXDirection(), TrevorLegacy.player.getYDirection(), TrevorLegacy.tileMap))
				{
					// Move the player so the pushing appears smooth :)
					// Only do this if moving the rock was successful though!
					TrevorLegacy.player.move(TrevorLegacy.player.getXDirection(), TrevorLegacy.player.getYDirection(), TrevorLegacy.tileMap);
				}
			}
		}
	}

	@Override
	public void update() 
	{
		pushClock.updateClock();
	}
	
}
